<?php
class PEValidator{
	/**
	* Array Used to save patterns
	*/
	private $patterns;
	/**
	* Boolean stores the validation result and used for farther validation
	*/
	private $success=true;
	/** 
	* Array saves the variable names that did not validate
	*/
	private $errors;
	
	/*
	* All the common patterns are provided as constant regular expression
	*/
	const PATTERN_NO_EMPTY='|.+|';
	const PATTERN_EMPTY='|^.{0}$|';
	const PATTERN_BASE64='|^[A-Za-z0-9\+/]+\={0,2}$|';
	
	/**
	 * Variable type is post
	 */
	const VTYPE_POST = 'POST';
	/**
	 * Variable type is get
	 */
	const VTYPE_GET = 'GET';
	/**
	 * Variable type is cookie
	 */
	const VTYPE_COOKIE = 'COOKIE';
	
	
	public  function __construct($patterns=NULL){
		$this->setPatterns($patterns);
		$this->success =true;
		$this->errors = array();
	}
	public function __destruct(){
		$this->patterns = null;
		$this->errors = null;
	}
	/**
	 * sets the regular expression patterns for each variable name group by whether cookie, get or post.
	 *
	 * @param  array $patterns an array of patterns...
	 * @example  
	 * $v = new Validator;
	 * $v->setPatterns(array(
	 * 	Validator::VTYPE_COOKIE=>array(
	 * 			'PHPSESSID' => '|^[a-fA-F0-9]+$|'
	 * 		),
	 * 	Validator::VTYPE_POST => array(
	 *	 		'name'=>'|^[a-zA-Z \.\-]+$|',
	 * 			'roll'=>'|^\d+$|'
	 * 		)
	 * 	)
	 * );
	 */
	public function setPatterns($patterns=NULL){
		if(!is_null($patterns)){
			$this->patterns = array();
			if(isset($patterns[self::VTYPE_COOKIE])){ $this->patterns[self::VTYPE_COOKIE]=$patterns[self::VTYPE_COOKIE];}
			if(isset($patterns[self::VTYPE_GET])){ $this->patterns[self::VTYPE_GET]=$patterns[self::VTYPE_GET];}
			if(isset($patterns[self::VTYPE_POST])){ $this->patterns[self::VTYPE_POST]=$patterns[self::VTYPE_POST];}
		}
	}
	/**
	 * Validates the values against pattern. Values and patterens must be setup before calling this method
	 *
	 * @return  boolean true if the validation was successfull.
	 */
	public function validate(){
		$this->success = true;
		foreach (array(self::VTYPE_POST,self::VTYPE_GET,self::VTYPE_COOKIE) as $v){
			$this->checkVariables($v);
		}
		return $this->success;
	}
	
	/**
	 * Checks if the previous validation operation was successfull
	 *
	 * @return boolean true if  previous validation operation was successfull
	 */
	public  function validationWasSuccessfull(){
		return $this->success;
	}
	/**
	 * Match a constant value $value againsta a regular expression pattern $pattern
	 *
	 * @param mixed $value value to check 
	 * @param  string $pattern pattern to use 
	 * @return boolean true if successfully matched otherwise false.
	 */
	private function matchPattern($value,$pattern){
		return preg_match($pattern,$value)?true:false;
	}
	/**
	 * Check variables of type $type. 
	 *
	 * @param string $type type can be any of post, get and cookie. If anything else is passed, Its simply ignored
	 */
	private function checkVariables($type){
		$type= strtolower($type);
		if(in_array($type,array(self::VTYPE_POST,self::VTYPE_GET,self::VTYPE_COOKIE))){
			if(isset($this->patterns[$type])){
				$this->errors[$type] = array();
				if(is_array($this->patterns[$type])){
					foreach($this->patterns[$type] as $var => $pat){
						if($this->matchPattern($GLOBALS['_'.$type][$var],$pat)){
							$this->success = $this->success && true;
						}else{
							$this->success = $this->success && false;
							$this->errors[$type][]=$var;
						}
					}
				}
			}
		}
	}
	
	/**
	 * Get the list of variables that were not validated
	 *
	 * @return array of unvalidated variables
	 */
	public function getUnvalidatedVars(){
		return $this->errors;
	}
}
?>